import Vue from 'vue'
import VueRouter, { RouteConfig } from 'vue-router'
import Layout from '@/views/layout/index.vue'
import store from '@/store'

Vue.use(VueRouter)
// 配置路由规则

const routes: Array<RouteConfig> = [
  {
    name: 'login',
    path: '/login',
    component: () => import(/* webpackChunkName: 'login' */'@/views/login/index.vue')
  },
  {
    name: 'layout',
    path: '/',
    component: Layout,
    meta: { // 父路由写过了 requiresAuth ，子路由就可以不用写了
      requiresAuth: true, // 自定义数据
      title: '首页'
    }, // meta 默认就是一个空对象，不写也行
    children: [
      {
        name: 'Courses',
        path: '',
        meta: {
          title: '课程管理'
        },
        component: () => import(/* webpackChunkName: 'Courses' */'@/views/course/index.vue')
      },
      {
        name: 'Courses',
        path: '/Courses',
        meta: {
          title: '课程管理'
        },
        component: () => import(/* webpackChunkName: 'Courses' */'@/views/course/index.vue')
      },
      {
        name: 'Advertise',
        path: '/Advertise',
        meta: {
          title: '广告管理'
        },
        component: () => import(/* webpackChunkName: 'Advertise' */'@/views/advert/index.vue')
      },
      {
        name: 'AdvertiseSpace',
        path: '/AdvertiseSpace',
        meta: {
          title: '广告位管理'
        },
        component: () => import(/* webpackChunkName: 'AdvertiseSpace' */'@/views/advert-space/index.vue')
      },
      {
        name: 'menu',
        path: '/menu',
        meta: {
          title: '菜单管理'
        },
        component: () => import(/* webpackChunkName: 'menu' */'@/views/menu/index.vue')
      },
      {
        path: '/menu/create',
        name: 'menu-create',
        meta: {
          title: '新增菜单'
        },
        component: () => import(/* webpackChunkName: 'menu-create-edit' */ '@/views/menu/create.vue')
      },
      {
        path: '/menu/:id/edit',
        name: 'menu-edit',
        meta: {
          title: '菜单编辑'
        },
        component: () => import(/* webpackChunkName: 'menu-create-edit' */ '@/views/menu/edit.vue')
      },
      {
        path: '/menu/allocMenu',
        name: 'alloc-menu',
        meta: {
          title: '允许菜单列表'
        },
        component: () => import(/* webpackChunkName: 'alloc-menu' */ '@/views/menu/allocMenu.vue')
      },
      {
        path: '/menu/allocResource',
        name: 'alloc-resource',
        meta: {
          title: '分配菜单'
        },
        component: () => import(/* webpackChunkName: 'alloc-resource' */ '@/views/menu/allocResource.vue')
      },
      {
        name: 'resource',
        path: '/resource',
        meta: {
          title: '资源管理'
        },
        component: () => import(/* webpackChunkName: 'resource' */'@/views/resource/index.vue')
      },
      {
        name: 'role',
        path: '/role',
        meta: {
          title: '角色管理'
        },
        component: () => import(/* webpackChunkName: 'role' */'@/views/role/index.vue')
      },
      {
        name: 'Users',
        path: '/Users',
        meta: {
          title: '用户管理'
        },
        component: () => import(/* webpackChunkName: 'Users' */'@/views/user/index.vue')
      }
    ]
  },
  {
    name: '404',
    path: '*',
    component: () => import(/* webpackChunkName: '404' */'@/views/err-page/404.vue')
  }
]

const router = new VueRouter({
  routes
})

// 路由前置守卫
// to 去哪里的路由信息
// from 从哪里来的路由信息
router.beforeEach((to, from, next) => {
  if (to.matched.some(record => record.meta.requiresAuth)) {
    // 只要有一级路由需要身份认证，就需要验证登录状态
    if (!store.state.user) {
      next({
        name: 'login',
        query: {
          redirect: to.fullPath // 把登录成功需要返回的页面告诉登录页面
        }
      })
    } else {
      store.commit('setHeaders', JSON.stringify(to.matched.map((item, index) => {
        if (index === to.matched.length - 1) {
          return {
            path: to.fullPath,
            title: item.meta.title
          }
        }
        return {
          path: item.path || '/',
          title: item.meta.title
        }
      })))
      next() // 允许通过
    }
  } else {
    next() // 确保一定要调用 next()
  }
})

export default router
